package cn.sunshine.org.controller;

import cn.sunshine.basic.annotation.PreAuthorize;
import cn.sunshine.basic.utils.AjaxResult;
import cn.sunshine.basic.utils.PageList;
import cn.sunshine.org.domain.Department;
import cn.sunshine.org.query.DepartmentQuery;
import cn.sunshine.org.service.IDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/dept")
public class DepatmentController {
    @Autowired
    private IDepartmentService departmentService;

    @GetMapping
    @PreAuthorize(name = "查询所有" ,sn = "dept:loadAll")
    public List<Department> loadAll(){

        return departmentService.loadAll();
    }
    /*
     * @Title: loadById
     * @Description: 根据id查询
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/23 17:42
     * @Parameters: [id]
     * @Return cn.sunshine.org.domain.Department
     */
    @GetMapping("/{id}")
    @PreAuthorize(name = "根据Id查询",sn = "dept:get")
    public Department loadById(@PathVariable("id") Long id) {

        return departmentService.loadById(id);
    }

    /*
     * @Title: queryData
     * @Description: 高级查询查询所有
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/23 17:43
     * @Parameters: [departmentQuery]
     * @Return cn.sunshine.basic.utils.PageList<cn.sunshine.org.domain.Department>
     */

    @PostMapping
    @PreAuthorize(name = "高级查询查询所有" ,sn = "dept:queryData")
    public PageList<Department> queryData(@RequestBody DepartmentQuery departmentQuery) {
        System.out.println(departmentQuery);
        return departmentService.queryData(departmentQuery);
    }

    /*
     * @Title: remove
     * @Description: 根据id删除数据
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/23 17:58
     * @Parameters: [id]
     * @Return cn.sunshine.basic.utils.AjaxResult
     */
    @DeleteMapping("/{id}")
    @PreAuthorize(name = "删除部门" ,sn = "dept:remove")
    public AjaxResult remove(@PathVariable("id") Long id) {
        try {
            departmentService.remove(id);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，请重试");
        }
    }

    /*
     * @Title: addOrUpdate
     * @Description: 增加与修改
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/23 18:17
     * @Parameters: [department]
     * @Return cn.sunshine.basic.utils.AjaxResult
     */
    @PutMapping
    @PreAuthorize(name = "新增或者修改部门" ,sn = "dept:addOrUpdate")
    public AjaxResult addOrUpdate(@RequestBody Department department) {
        try {
            if (department.getId() == null) {
                departmentService.save(department);

            } else {
                departmentService.update(department);

            }
            return AjaxResult.getAjaxResult();

        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，请重试");
        }
    }

    /*
     * @Title: batchDepartment
     * @Description: 批量删除部门数据
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/25 0:25
     * @Parameters: [ids]
     * @Return cn.sunshine.basic.utils.AjaxResult
     */
    @PatchMapping
    @PreAuthorize(name = "批量删除部门数据" ,sn = "dept:batchDepartment")
    public AjaxResult batchDepartment(@RequestBody List<Long> ids) {
        try {// 调用删除方法，根据id删除
            departmentService.batchRemove(ids);
            return AjaxResult.getAjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.getAjaxResult().setSuccess(false).setMessage("系统繁忙，请重试");

        }
    }

    /*
     * @Title: findDepartmentTree
     * @Description: 创建部门极树接口
     * @Author: Mr.Jiang
     * @Version: 1.0
     * @Date:  2022/6/26 10:20
     * @Parameters: []
     * @Return list<Department>
     */
    @GetMapping("/findDepartmentTree")
    @PreAuthorize(name = "创建部门极树接口" ,sn = "dept:findDepartmentTree")
    public List<Department> findDepartmentTree() {
        List<Department> departmentTree = departmentService.findDepartmentTree();
        departmentTree.forEach(System.out::println);
        return departmentTree;
    }
}
